<?php

/**
 * players actions.
 *
 * @package    ghostly
 * @subpackage players
 * @author     Stefan Moonen <stefan@qonstrukt.nl>
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class playersActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $display_length = $request->getParameter('iDisplayLength', sfConfig::get('app_games_rows_per_page'));
    $display_start = $request->getParameter('iDisplayStart', 0) / $display_length + 1;

    $this->pager = new sfDoctrinePager(
      'Game',
      $display_length
    );
    $this->pager->setQuery(PlayerTable::getInstance()->getOverviewQuery());
    $this->pager->setPage($display_start);
    $this->pager->init();

    if ($request->isXmlHttpRequest())
    {
      $data = array();
      foreach ($this->pager->getResults() as $row)
      {
        $data[] = array(
          $row->name,
          $row->gamecount,
          $row->spoofedrealm
        );
      }

      $output = array(
        'echo' => $request->getParameter('echo'),
        'iTotalRecords' => $this->pager->getNbResults(),
        'iTotalDisplayRecords' => $this->pager->getNbResults(),
        'aaData' => $data
      );

      exit(json_encode($output));
    }
  }

 /**
  * Executes detail action
  *
  * @param sfRequest $request A request object
  */
  public function executeDetail(sfWebRequest $request)
  {
    $this->gameplayers = $this->getRoute()->getObjects();
    $this->forward404If($this->gameplayers->count() == 0, 'No player exists by that name.');

    $this->playername = $this->gameplayers->getFirst()->name;
  }
}
